package com.imti.webdemo;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity
{

    private static final String TAG = MainActivity.class.getSimpleName();

    private WebView webViewTest;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView()
    {
        webViewTest = findViewById(R.id.webview_test);
        ProgressBar progressBar = findViewById(R.id.progress_loading);
        ProgressBar progressHorizon = findViewById(R.id.progress_horizontal_loading);

        //加载服务端网页URL
        webViewTest.loadUrl("http://www.toutiao.com");
        //加载本地网页
//        webViewTest.loadData("html字符串","text/html","加密方式，如base64");

        //设置网页中是否允许执行JavaScript
        webViewTest.getSettings().setJavaScriptEnabled(true);
        //设置允许JavaScript打开新页面
        webViewTest.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        //参数1：JavaScript所调用的接口对象
        //参数2：JavaScript所获取的接口对象别名
        webViewTest.addJavascriptInterface(new JSInterface(this), "JSInterface");

        //原生主动调用JavaScript方法
        webViewTest
            .evaluateJavascript("javascript:getLogin(" + "参数" + ")",
                new ValueCallback<String>()
                {
                    @Override
                    public void onReceiveValue(String value)
                    {

                    }
                });

        //WebView的JavaScript相关配置
        webViewTest.setWebChromeClient(new WebChromeClient()
        {
            //监听网页加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress)
            {
                super.onProgressChanged(view, newProgress);

                //显示网页加载进度
                progressHorizon.setProgress(newProgress);
            }
        });

        webViewTest.setWebViewClient(new WebViewClient()
        {
            //网页开始加载
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {
                super.onPageStarted(view, url, favicon);

                progressBar.setVisibility(View.VISIBLE);
                progressHorizon.setVisibility(View.VISIBLE);
            }

            //网页加载完成
            @Override
            public void onPageFinished(WebView view, String url)
            {
                super.onPageFinished(view, url);

                progressBar.setVisibility(View.GONE);
                progressHorizon.setVisibility(View.GONE);
            }

            //防止WebView加载网页后，跳转到系统浏览器
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
            {
                //当前本地WebView加载URL
                view.loadUrl(request.getUrl().toString());

                return true;
            }
        });
    }

    //监听系统返回按钮
    @Override
    public void onBackPressed()
    {
        //判断网页是否可以返回上一步
        if (webViewTest.canGoBack())
        {
            webViewTest.goBack();
        } else
        {
            super.onBackPressed();
        }
//        if(webViewTest.canGoForward())
//            webViewTest.goForward();

    }
}